<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use think\exception\ValidateException;
use app\middleware\JWTAUTH;
require_once('../../../extends/JWT.php');

class Edit extends BaseController
{

    protected $middleware=['JWTAUTH'];

    public function index(){
        if($this->request->action()=='post'){
            //查询是否有同名存在，保证名字的唯一性
            $exist_users=Db::table('User')->where('name',$this->request->param('name'))->find();
            foreach($exist_users as $exist_user){
                if($exist_user['id'] == token_user_id){
                    continue;
                }else{
                    return json_encode(array(
                        'status' => '104',
                        'message' => '用户名已存在',
                    ));
                }
            }
            $cur_user=User::find(array('id'=>token_user_id));  //通过token的userid取得
            if($cur_user->id)
            $user_edit_data=array(
              'name' => $this->request->param('name'),  //通过token取得
              'password' => $this->request->param('password'),
            );
            $cur_user->save($user_edit_data);
            return json_encode(array(
              'status' => '90',
              'message' => '用户信息修改成功',
            ));
        }
    }
}
